home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
studset.sim
< prev
next >
Wrap
Text File
|
1993-08-16
|
13KB
|
333 lines
% ****************************************************************
% * *
% * This is the program constructed in example 14.1 of *
% * Object Oriented Programming with Simula by Bj|rn Kirkerud; *
% * *
% ****************************************************************
external class Settools;
Settools begin
% ****************************************************************
% * *
% * Declarations of auxiliary procedures: *
% * *
% ****************************************************************
character procedure prompt_for_char(prompt); text prompt;
begin
outtext(prompt); breakoutimage; inimage;
prompt_for_char := inchar;
end of prompt_for_char;
integer procedure prompt_for_int(prompt); text prompt;
begin
outtext(prompt); breakoutimage; inimage;
prompt_for_int := inint;
end of prompt_for_int;
real procedure prompt_for_real(prompt); text prompt;
begin
outtext(prompt); breakoutimage; inimage;
prompt_for_real := inreal;
end of prompt_for_real;
text procedure prompt_for_text(prompt); text prompt;
begin
outtext(prompt); breakoutimage; inimage;
prompt_for_text :- intext(80).strip;
end of prompt_for_text;
Boolean procedure prompt_for_bool(prompt); text prompt;
begin character c;
outtext(prompt); breakoutimage; inimage;
c := inchar;
prompt_for_bool := c = 'y' or c = 'Y';
end of prompt_for_bool;
procedure User_message(message); text message;
begin outtext(message); outimage end;
% ****************************************************************
% * *
% * The class Student: *
% * *
% ****************************************************************
element class Student(ident); text ident;
begin
integer year, month, day, form;
Boolean female;
character math_grade, eng_grade, hist_grade;
text procedure key; key :- ident;
! A variable to hold a reference to the next Student in a pointer chain: ;
ref(Student) next_in_chain;
procedure read;
begin
year := prompt_for_int("Year of birth? ");
month := prompt_for_int("Month? ");
day := prompt_for_int("Day? ");
form := prompt_for_int("Form? ");
female := prompt_for_bool("Female? ");
math_grade := prompt_for_char("Grade in mathematics? ");
eng_grade := prompt_for_char("Grade in English? ");
hist_grade := prompt_for_char("Grade in history? ");
end of Student'read;
procedure display;
begin
outtext("Data for student: "); outtext(ident);
outtext(". Born: "); outint(day, 2); outchar('/');
outint(month, 2); outchar('/');
outint(year, 4);
outtext(if female then ". Female." else ". Male."); outimage;
outtext(" Form: "); outint(form, 1);
outtext(". Current grades:");
outtext(" Mathematics: "); outchar(math_grade);
outtext(" English: "); outchar(eng_grade);
outtext(" History: "); outchar(hist_grade); outimage;
end of Student'display;
procedure change;
begin character attribute;
attribute := prompt_for_char("What do you want to change? ");
if attribute = 'y' then year
:= prompt_for_int("New birth year? ") else
if attribute = 'm' then month
:= prompt_for_int("New birth month? ") else
if attribute = 'd' then day
:= prompt_for_int("New day of birth? ") else
if attribute = 'f' then form
:= prompt_for_int("New form number? ") else
if attribute = 's' then female
:= prompt_for_bool("Female? ") else
if attribute = 'a' then math_grade
:= prompt_for_char("New grade in math? ") else
if attribute = 'e' then eng_grade
:= prompt_for_char("New grade in English? ") else
if attribute = 'h' then hist_grade
:= prompt_for_char("New grade in history? ")
else begin
User_message("You can change one of the following attributes:");
User_message(" m: Birth month");
User_message(" d: Day of birth");
User_message(" f: Form number");
User_message(" s: Sex");
User_message(" a: Grade in mathematics");
User_message(" e: Grade in English");
User_message(" h: Grade in history");
change; ! Observe that this is an invocation of the procedure
! being declared. The effect is that user is given
! another chance to change;
end;
end of Student'change;
character procedure worst_grade;
worst_grade := max(math_grade, max(eng_grade, hist_grade));
procedure put_in_record(outf); ref(outfile) outf;
inspect outf do
begin
outtext(ident); setpos(11);
outint(year, 5); outint(month, 3); outint(day, 3);
outint(form, 2); outint(if female then 1 else 0, 2);
outchar(math_grade); outchar(eng_grade); outchar(hist_grade);
end;
procedure get_from_record(inf); ref(infile) inf;
inspect inf do
begin
year := inint; month := inint; day := inint;
form := inint; female := inint = 1;
math_grade := inchar; eng_grade := inchar; hist_grade := inchar;
end;
end of Student;
% ****************************************************************
% * *
% * Declaration of a variable to keep a reference to *
% * an ordered set of Students: *
% * *
% ****************************************************************
ref(ordered_set) The_students;
% ****************************************************************
% * *
% * Declarations of command procedures: *
% * *
% ****************************************************************
procedure Give_help;
begin
User_message("The legal commands are: ");
User_message(" ?: Help (writes this text)");
User_message(" N: To enter data about a new student");
User_message(" W: Writes data about a specified student");
User_message(" L: Writes a list with all students");
User_message(" C: Changes data about a specified student");
User_message(" R: Removes all data about a specified student");
User_message(" P: Puts all data to file ""stud.dta""");
User_message(" G: Gets data from file ""stud.dta""");
User_message(" B: Writes students with bad grades");
User_message(" Q: Quit (the program execution stops)");
end of Give_help;
procedure Enter_student;
begin ref(Student) a_student; Boolean ident_exists;
a_student :- new Student(prompt_for_text("Identity number? "));
The_students.add_element(a_student, ident_exists);
if ident_exists
then User_message("The identity number " & a_student.key &
" is already in use!")
else begin
a_student.read;
User_message("The data for student " & a_student.key &
" have been stored.");
end;
end of Enter_student;
procedure Write_student;
begin ref(Student) a_student;
a_student :- The_students.find_element(prompt_for_text("Id. number? "));
if a_student == none
then User_message("No student with that identity number!")
else a_student.display;
end of Write_student;
procedure List_students;
begin ref(Student) a_student;
User_message("The students for which data have been entered:");
a_student :- The_students.first_element;
while a_student =/= none do
begin
a_student.display;
a_student :- The_students.next_element;
end;
end of List_students;
procedure Change_student;
begin ref(Student) a_student;
a_student :- The_students.find_element(prompt_for_text("Id. number? "));
if a_student == none
then User_message("No student with that identity number!")
else begin a_student.display; a_student.change end;
end of Change_student;
procedure Remove_student;
begin integer ident_number; Boolean no_such_student;
ident_number := prompt_for_int("Identity number? ");
The_students.Remove_element(prompt_for_text("Id. number? "),
no_such_student);
if no_such_student
then User_message("No student with that identity number!")
else User_message("The student has been removed!");
end of Remove_student;
procedure Put_to_file;
begin ref(Student) a_student;
inspect new outfile("stud.dta") do
begin
open(blanks(28));
a_student :- The_students.first_element;
while a_student =/= none do
begin
a_student.put_in_record(this outfile);
outimage;
a_student :- The_students.next_element;
end;
close;
end;
end of Put_to_file;
procedure Get_from_file;
begin ref(Student) a_student; Boolean ident_exists;
inspect new infile("stud.dta") do
begin
open(blanks(28)); inimage;
while not endfile do
begin
a_student :- new Student(intext(10).strip);
a_student.get_from_record(this infile);
The_students.add_element(a_student, ident_exists);
inimage;
end;
close;
end;
end of Get_from_file;
procedure Bad_grades;
begin character grade_limit; ref(Student) a_student;
grade_limit := prompt_for_char("Grade limit? ");
a_student :- The_students.first_element;
while a_student =/= none do
begin
if a_student.worst_grade ge grade_limit then a_student.display;
a_student :- The_students.next_element;
end;
end of Bad_grades;
procedure Unknown_command(c); character c;
begin
outtext(" You gave the command '"); outchar(c);
outtext("'. This is not among the legal commands."); outimage;
outtext(" Type ? if you don't remember the legal commands");
outimage;
end of Unknown command;
% ****************************************************************
% * *
% * Declaration of a variable to keep the latest command *
% * typed by the user: *
% * *
% ****************************************************************
character command;
% ****************************************************************
% * *
% * That was the last declaration. *
% * Now come the imperatives of the program: *
% * *
% ****************************************************************
% * First, initialization of the data-structure: ;
The_students :- new ordered_set;
% * Then, read and execute commands typed by the user: ;
command := prompt_for_char("Type your first command (? for help) > ");
while command ne 'Q' do
begin
if command = '?' then Give_help else
if command = 'N' then Enter_student else
if command = 'W' then Write_student else
if command = 'L' then List_students else
if command = 'C' then Change_student else
if command = 'R' then Remove_student else
if command = 'P' then Put_to_file else
if command = 'G' then Get_from_file else
if command = 'B' then Bad_grades
else Unknown_command(command);
command := prompt_for_char("Your next command > ");
end;
User_message("Bye");
end